package com.rstrulak.users.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

import com.rstrulak.dao.UserDao;

@Service
public class UserAuthenticationProvider extends DaoAuthenticationProvider {
    /**
     * User data access object.
     */
    @Autowired
    private UserDao userDao = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        Authentication auth = null;
        try {
            auth = super.authenticate(authentication);
//            if (auth.getPrincipal() != null && auth.getPrincipal() instanceof User) {
//                User user = (User) auth.getPrincipal();
//                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//                userDao.saveOrUpdate(user);
//
//            }
        } catch (AuthenticationException e) {
            throw e;
        }
        return auth;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}
